package org.luxor.commons.websocket.config;

import org.luxor.commons.redis.client.RedisTopicClient;
import org.luxor.commons.redis.config.RedisConfig;
import org.luxor.commons.websocket.component.session.WsShareProtocolReceiver;
import org.luxor.commons.websocket.component.session.WsShareSessionStorage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * websocket 共享会话配置
 *
 * @author Mr.yan  @date 2021/9/5
 */
@Order(999)
@Configuration
@AutoConfigureAfter(RedisConfig.class)
@ConditionalOnClass(RedisTopicClient.class)
public class WsShareSessionConfig {
    private static final Logger logger = LoggerFactory.getLogger(WsShareSessionConfig.class);

    public WsShareSessionConfig() {
        System.out.println("123123");
    }

    @Bean
    public WsShareSessionStorage wsSessionStorage(ObjectProvider<RedisTopicClient> redisTopicClient) {
        logger.info("Initialize wsShareSession Bean of type WsShareSessionStorage.");
        return new WsShareSessionStorage(redisTopicClient.getObject());
    }

    @Bean
    public WsShareProtocolReceiver wsShareProtocolReceiver(WsShareSessionStorage wsSessionStorage) {
        logger.info("Initialize wsShareProtocolReceiver Bean.");
        return new WsShareProtocolReceiver(wsSessionStorage);
    }
}
